package com.rdc.qingkou.view.fragment;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rdc.qingkou.R;

import java.io.IOException;
import java.net.URL;

/**
 * Created by asus on 18-1-19.
 */

public class ShareViewFragmentwo extends Fragment {
    private int mId;
    private TextView mTextView;
     private  String mText;
     private String mImageUrl;
     private  String mViewUrl;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.send_template_two, container, false);
         mTextView = (TextView)view.findViewById(R.id.layout_card_content_textView);

        mTextView.setText(mText);
        new DownloadImageTask().execute(mImageUrl) ;



        return view;
    }

    @Nullable
    @Override
    public View getView() {
        return super.getView();
    }

    public void setTextView(String text){
       mText = text;
    }

    public void  setImageView(String url){
        mImageUrl = url;
    }
    public  String getViewUrl(){
        return  mViewUrl;
    }
    private Drawable loadImageFromNetwork(String imageUrl)
    {
        Drawable drawable = null;
        try {
            // 可以在这里通过文件名来判断，是否本地有此图片
            drawable = Drawable.createFromStream(
                    new URL(imageUrl).openStream(), "image.jpg");
        } catch (IOException e) {
            Log.d("test", e.getMessage());
        }
        if (drawable == null) {
            Log.d("test", "null drawable");
        } else {
            Log.d("test", "not null drawable");
        }

        return drawable ;
    }


    private class DownloadImageTask extends AsyncTask<String, Void, Drawable>
    {

        protected Drawable doInBackground(String... urls) {
            return loadImageFromNetwork(urls[0]);
        }

        protected void onPostExecute(Drawable result) {
            mTextView.setBackground(result);
        }
    }
}
